package com.gitee.wsl.compose.chart.xy

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import com.gitee.wsl.compose.chart.xy.axis.AxisModel
import com.gitee.wsl.compose.chart.xy.axis.AxisState
import com.gitee.wsl.compose.chart.xy.model.PointEntry
import com.gitee.wsl.compose.ui.container.hoverable.HoverableElementAreaScope

/**
 * A scope for XY plots providing axis and state context.
 */
 interface XYGraphScope<X, Y> : HoverableElementAreaScope {
     val xAxisModel: AxisModel<X>
     val yAxisModel: AxisModel<Y>
     val xAxisState: AxisState
     val yAxisState: AxisState

    /**
     * Transforms [point] from [AxisModel] space to display coordinates provided a plot area [size].
     */
     fun scale(
        point: PointEntry<X, Y>,
        size: Size
     ): Offset {
        return Offset(
            xAxisModel.computeOffset(point.x) * size.width,
            size.height - yAxisModel.computeOffset(point.y) * size.height
        )
    }
}

class XYGraphScopeImpl<X, Y>(
    override val xAxisModel: AxisModel<X>,
    override val yAxisModel: AxisModel<Y>,
    override val xAxisState: AxisState,
    override val yAxisState: AxisState,
    val hoverableElementAreaScope: HoverableElementAreaScope
) : XYGraphScope<X, Y>, HoverableElementAreaScope by hoverableElementAreaScope